home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0065_A definite Integral.pas < prev    next >
Pascal/Delphi Source File  |  1994-02-22  |  4KB  |  120 lines

  1. program integration; uses crt;
  2.  
  3.   { program below demonstrates Pascal code used to compute a definite }
  4.   { integral.  Useful for many calculus-related functions such as     }
  5.   { finding areas of irregular shapes when a functional relation is   }
  6.   { known.  You may freely use this code, but do please give me the   }
  7.   { credits.                                                          }
  8.  
  9.   { A negative area as an answer, is the result of incorrectly defining
  10.   the lower and upper bounds for a function.  For example, using the
  11.   function
  12.  
  13.     6 - 6x^5, a perfectly justifiable lower bound would be 0, while - 5
  14.     would not be.  a perfectly justifiable upper bound would be 1, while
  15.     6 would not be.  The non-justifiable bounds used as examples, are not
  16.     defined in the function used, so a negative area would result in this
  17.     case
  18.  
  19.   { Tutorial: this program uses Simpson's rule as a method of finding  }
  20.   { the area under a graphed curve.  A lower and an upper limit is set }
  21.   { where the area is calculated.  The area is cut up into a number of }
  22.   { rectangles dictated by the 'number of divisions'.  The more you    }
  23.   { divide up this area, the more accurate an approximation becomes.   }
  24.  
  25.   var
  26.     lower, upper, divisions, sum, width, counter, x, left, right, middle,
  27.       c: real;
  28.  
  29.   procedure formula;
  30.  
  31.     { procedure set apart from rest of program for ease of changing the }
  32.     { function if need be.   The function is defined as: f(x) =         }
  33.     { <expression>, expression being set in a Pascal-type statement     }
  34.  
  35.     begin
  36.       c := 6 - ( 6 * x * x * x * x * x ); { current function set: 6 - 6x^5 }
  37.     end;
  38.  
  39.   begin
  40.  
  41.     clrscr;
  42.     { read in lower bound }
  43.  
  44.     writeln('Input lower limit.');
  45.     readln(lower);
  46.  
  47.     { read in upper bound }
  48.  
  49.     writeln('Input upper limit.');
  50.     readln(upper);
  51.  
  52.     { read in the number of divisions.. The higher you make this number, }
  53.     { the more accurate the results, but the longer the calculation...   }
  54.  
  55.     Writeln('number of divisions?');
  56.     readln(divisions);
  57.  
  58.     { set the total sum of the rectangles to zero }
  59.  
  60.     sum := 0;
  61.  
  62.     { determine width of each rectangle }
  63.  
  64.     width := (upper - lower) / (2 * divisions);
  65.  
  66.     { initalize counter for divisions loop }
  67.  
  68.     counter := 1;
  69.  
  70.     clrscr;
  71.     writeln('Working...');
  72.  
  73.     { start computations }
  74.  
  75.     repeat
  76.  
  77.       { define left, right, and middle points along each rectangle }
  78.  
  79.       left := lower + 2 * (counter - 1) * width;
  80.       right := lower + 2 * counter * width;
  81.       middle := (left + right) / 2;
  82.  
  83.       { compute functional values at each point }
  84.  
  85.       x := left;
  86.       formula;
  87.       left := c;
  88.       x := middle;
  89.       formula;
  90.       middle := c;
  91.       x := right;
  92.       formula;
  93.       right := c;
  94.  
  95.       { calculate particular rectangle area and increment the area to the }
  96.       { sum of the areas.                                                 }
  97.  
  98.       sum := (width * (left + 4 * middle + right)) / 3 + sum;
  99.  
  100.       { write sum to screen as a "working" status }
  101.  
  102.       writeln;
  103.       write(sum:0:9);
  104.       gotoxy(1,2);
  105.  
  106.       { increment counter }
  107.  
  108.       counter := counter + 1;
  109.  
  110.     { stop loop when all areas of rectangles are computed }
  111.  
  112.     until counter = divisions;
  113.  
  114.     { output results }
  115.  
  116.     clrscr;
  117.     writeln('The area under the curve is ', sum:0:9, '.');
  118.                                           { ^^^^^^^^ }
  119.   end.                                    { format code used to eliminate }
  120.                                           { scientific notation in answer }